//
//  TFTabBar.swift
//  weiboSina
//
//  Created by admin on 15/12/27.
//  Copyright © 2015年 admin. All rights reserved.
//

import UIKit

class TFTabBar: UITabBar {

    var composeButtonClickClourse: (() -> ())?
    
   //重写init方法
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        addSubview(composeButton)
        
        backgroundImage = UIImage(named: "tabbar_background")
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
    override func layoutSubviews() {
        super.layoutSubviews()
        
        composeButton.center.x = self.bounds.width * 0.5
        composeButton.center.y = self.bounds.height * 0.5
        
        let childW = bounds.size.width * 0.2
        
        var index = 0
        for value in subviews{
            if value.isKindOfClass(NSClassFromString("UITabBarButton")!){
            
            value.frame.origin.x = CGFloat(index)*childW
            value.frame.size.width = childW
                
                index++
                
                if index == 2 {
                
                index++
                
                }
            }
        
        
        }
        
    }
    
    //监听事件的点击
   @objc private func composeButtonClick(){
    
        composeButtonClickClourse?()
    
    
    }
    
    
    //Mark: - 懒加载控件
    //private 是私有方法。如果不想把某些属性或者方法暴露给外界，使用此修饰
    private lazy var composeButton: UIButton = {() -> (UIButton) in
        
        let button = UIButton()
        
        button.addTarget(self, action: "composeButtonClick", forControlEvents: .TouchUpInside)
        
        button.setBackgroundImage(UIImage(named: "tabbar_compose_button_highlighted"), forState: .Highlighted)
        button.setBackgroundImage(UIImage(named: "tabbar_compose_button"), forState: .Normal)
    
        button.setImage(UIImage(named: "tabbar_compose_icon_add_highlighted"), forState: .Highlighted)
        button.setImage(UIImage(named: "tabbar_compose_icon_add"), forState: .Normal)
            
        button.sizeToFit()
        return button
    }()

}
